<?
function timedate()
{
	echo "<table>";
	exec("/bin/date +%F", $out);
	exec("/bin/date +%X", $out);
	echo "<tr ".help('time_date')."><td><input type=text class=\"input-box\" name=date value=\"$out[0]\" onFocus=\"timedate_stop_date()\" onChange=\"timedate_update()\" onBlur=\"setTimeout('timedate_show_date()', 1000)\"></td></tr>";
	echo "<tr ".help('time_time')."><td><input type=text class=\"input-box\" name=time value=\"$out[1]\" onFocus=\"timedate_stop_time()\" onChange=\"timedate_update()\" onBlur=\"setTimeout('timedate_show_time()', 1000)\"></td></tr>";
	echo "</table>";
	echo '
<script type="text/javascript">
<!--
var timedata_time

function timedate_update() {
	var timedata_org_time = document.pageform.time.value.split( ":" )
	var timedata_org_date = document.pageform.date.value.split( "-" )
	if ( isNaN(timedata_org_time[2]) )
		timedata_org_time[2] = "00"
	timedate_time_tmp = new Date( timedata_org_date[0], timedata_org_date[1]-1, timedata_org_date[2], timedata_org_time[0], timedata_org_time[1], timedata_org_time[2])
	if ( !isNaN(timedate_time_tmp) )
		timedate_time = timedate_time_tmp
}

function timedate_tic() {
	timedate_time = new Date(timedate_time.getTime() + 1000)
	timedate_tic_time_id = setTimeout("timedate_tic()", 1000)
}

function timedate_show_time() {
	var timedate_hours = timedate_time.getHours()
	var timedate_minutes = timedate_time.getMinutes()
	var timedate_seconds = timedate_time.getSeconds()
	if (timedate_hours<=9)
		timedate_hours = "0"+timedate_hours
	if (timedate_minutes<=9)
		timedate_minutes = "0"+timedate_minutes
	if (timedate_seconds<=9)
		timedate_seconds = "0"+timedate_seconds
	document.pageform.time.value = timedate_hours+":"+timedate_minutes+":"+timedate_seconds
	timedate_show_time_id = setTimeout("timedate_show_time()", 1000)
}

function timedate_show_date() {
	var timedate_year = timedate_time.getFullYear()
	var timedate_month = timedate_time.getMonth() + 1
	var timedate_day = timedate_time.getDate()
	if (timedate_month<=9)
		timedate_month = "0"+timedate_month
	if (timedate_day<=9)
		timedate_day = "0"+timedate_day
	document.pageform.date.value = timedate_year+"-"+timedate_month+"-"+timedate_day
	timedate_show_date_id = setTimeout("timedate_show_date()", 1000)
}

function timedate_stop_time() {
	clearTimeout( timedate_show_time_id )
}

function timedate_stop_date() {
	clearTimeout( timedate_show_date_id )
}

timedate_update()
timedate_tic_time_id = setTimeout("timedate_tic()", 1000)
timedate_show_time()
timedate_show_date()
-->
</script>
';
}

function net()
{
	echo '<table>';
	echo '<tr><td>';

	$name="";
	$workgroup="";
	$lines = file('/etc/samba/smb.conf');
	foreach( $lines AS $line ) {
		if( preg_match( "/^\s*netbios name\s*=\s*(.*)$/", $line, $match ) ) {
			$name = trim($match[1]);
		}
		if( preg_match( "/^\s*workgroup\s*=\s*(.*)$/", $line, $match ) ) {
			$workgroup = trim($match[1]);
		}
	}

	echo '<table>';
	echo '<tr><td class="tbhead" colspan=2>Windowsnätverk</td></tr>';
	echo '<tr><td>Nätverksnamn</td><td><input name="netbiosname" type="text" value="'.$name.'"></td></tr>';
	echo '<tr><td>Arbetsgrupp</td><td><input name="workgroup" type="text" value="'.$workgroup.'"></td></tr>';
	echo '</table><br>';
	
	exec("cat /etc/network/interfaces | grep \"eth0 inet dhcp\"", $duh, $dhcp);
	$dhcp = !$dhcp;
	
	exec("/sbin/ifconfig eth | grep \"inet addr:\" | cut -d \":\" -f 2 | cut -d \" \" -f 1", $ip, $err);
	if ($err != 0)
		echo "Underligt fel, kan inte hämta information om ipadress";
	exec("/sbin/ifconfig eth | grep \"inet addr:\" | cut -d \":\" -f 4 | cut -d \" \" -f 1", $mask, $err);
	if ($err != 0)
		echo "Underligt fel, kan inte hämta information om nätmask";
	exec("/sbin/route -n | grep \"^0.0.0.0\" | cut -d \" \" -f 10", $route, $err);
	if ($err != 0)
		echo "Underligt fel, kan inte hämta information om default router";
	exec("/bin/cat /etc/resolv.conf | grep nameserver | cut -d \" \" -f 2", $ns, $err);
	if ($err != 0)
		echo "Underligt fel, kan inte hämta information om namnserver";

	if( !isset($ns[0]) )
		$ns[0] = "";
	if( !isset($ns[1]) )
		$ns[1] = "";

	echo "<table>";
	echo '<tr><td class="tbhead" colspan=2>Avancerade inställningar</td></tr>';
	  //dhcp är aktivt, skugga andra val
	if ($dhcp)
		echo "<tr><td ".help('net_dhcp').">Automatiskt/DHCP</td><td ".help('net_dhcp')."><input type=\"checkbox\" name=\"dhcp\" checked onClick=\"net_changedhcp()\"></td>";
	else
		echo "<tr><td ".help('net_dhcp').">Automatiskt/DHCP</td><td ".help('net_dhcp')."><input type=\"checkbox\" name=\"dhcp\" onClick=\"net_changedhcp()\"></td>";

	echo "<tr><td>IPadress</td><td><input type=text class=\"input-box\" name=ip value=\"$ip[0]\"></td></tr>";
	echo "<tr><td>Nätmask</td><td><input type=text class=\"input-box\" name=netmask value=\"$mask[0]\"></td></tr>";
	echo "<tr><td>Default router</td><td><input type=text class=\"input-box\" name=defaultgw value=\"$route[0]\"></td></tr>";
	echo "<tr><td>DNS1</td><td><input type=text class=\"input-box\" name=dns1 value=\"$ns[0]\"></td></tr>";
	echo "<tr><td>DNS2</td><td><input type=text class=\"input-box\" name=dns2 value=\"$ns[1]\"></td></tr>";
        echo "</table>";

	echo '</td><td style="padding:0.4em; vertical-align:top">';
	echo "<b style=\"color: #f00\">VARNING!!</b><br>Ändringar här kan få allvarliga konsekvenser, ändra enbart om du kan datornätverk.<br>Alla program och användare som är anslutna till servern riskerar att tappa kontakten och osparad data gå förlorad.";
	echo '</td></tr>';
	echo '</table>';

	echo '
<script type="text/javascript">
<!--
function net_changedhcp() {
	if (document.pageform.dhcp.checked==true) {
		document.pageform.ip.disabled=true
		document.pageform.netmask.disabled=true
		document.pageform.defaultgw.disabled=true
		document.pageform.dns1.disabled=true
		document.pageform.dns2.disabled=true
	}
	else {
		document.pageform.ip.disabled=false
		document.pageform.netmask.disabled=false
		document.pageform.defaultgw.disabled=false
		document.pageform.dns1.disabled=false
		document.pageform.dns2.disabled=false
	}
}
net_changedhcp();
//-->
</script>
';
}
	
function upgrade()
{
	/*
	UPPGRADERINGAR
	läser sträng från cron.d/upgrade
	*/
	if( file_exists("/etc/cron.d/upgrade") ) {
		$crons = explode(" ",@file_get_contents("/etc/cron.d/upgrade"));
		echo "<input type=\"checkbox\" name=\"autoupdate\" onClick=\"upgrade_update()\" checked> Automatiska uppdateringar<br>\n";
	}
	else {
		echo "<input type=\"checkbox\" name=\"autoupdate\" onClick=\"upgrade_update()\"> Automatiska uppdateringar<br>\n";
	}
	echo "<table>";
	echo "<tr ".help('update_auto')."><td>uppgradera</td><td>";
		$out = "<select name=\"day\">
			<option value=*>Alla dagar
			<option value=mon>Måndagar
			<option value=tue>Tisdagar
			<option value=wed>Onsdagar
			<option value=thu>Torsdagar
			<option value=fri>Fredagar
			<option value=sat>Lördagar
			<option value=sun>Söndagar
			</select>
		";
		echo str_replace('value='.$crons[4].'>','selected value='.$crons[4].'>', $out);
	echo "</td><td>klockan ";
		$out = "<select name=\"hour\">
			<option value=1>01:00
			<option value=2>02:00
			<option value=3>03:00
			<option value=4>04:00
			<option value=5>05:00
			<option value=6>06:00
			<option value=7>07:00
			<option value=8>08:00
			<option value=9>09:00
			<option value=10>10:00
			<option value=11>11:00
			<option value=12>12:00
			<option value=13>13:00
			<option value=14>14:00
			<option value=15>15:00
			<option value=16>16:00
			<option value=17>17:00
			<option value=18>18:00
			<option value=19>19:00
			<option value=20>20:00
			<option value=21>21:00
			<option value=22>22:00
			<option value=23>23:00
			<option value=24>24:00
			<option value=\"*\">Varje timme
		";
		echo str_replace('value='.$crons[1].'>','selected value='.$crons[1].'>', $out);	
	echo "</td></tr>";
	//echo "<tr><td>&nbsp;</td></tr>";
	echo "<tr><td colspan=3><input type=\"submit\" name=\"b_unow\" value=\"Uppgradera Nu\" class=\"buttonlong\" / ".help('update_button')."></td></tr>";
	echo "</table>\n";
	echo '
<script type="text/javascript">
<!--
function upgrade_update() {
	if( document.pageform.autoupdate.checked ) {
		document.pageform.day.disabled = false
		document.pageform.hour.disabled = false
	}
	if( !document.pageform.autoupdate.checked ) {
		document.pageform.day.disabled = true
		document.pageform.hour.disabled = true
	}
}
upgrade_update()
-->
</script>
';
}


function administrator()
{
	/*
	BYTE AV ADMINISTRATÖRENS LÖSEN OCH EMAIL
	*/
	require "../inc/rwINIfile.php";
	global $POSTCONF;
	global $ADMINMAILCONF;
	global $ISPCONF;

	$settings = readConf( $POSTCONF );
	$isps = readConf( $ISPCONF );
	$email = @file( $ADMINMAILCONF );
	$email = trim($email[0]);
	$relayhost = trim($settings["relayhost"]);
	echo "<table>\n";
	echo '<tr '.help('adminmail').'><td colspan="2" class="tbhead">Felmeddelanden via mail</td></tr>';
	echo "  <tr ".help('adminmail')."><td>Emailadress</td><td><input type=\"text\" class=\"input-box\" name=\"adminmail\" value=\"$email\"></td></tr>\n";
	echo '  <tr '.help('adminmail').'><td>Skicka mail via (mailrelay)</td>';

	echo '   <td><select name="isp" onChange="base_isp_update()">';
	$str = '';
	foreach( $isps AS $path => $name )
		$str .= '<option name="'.$name.'" value="'.$path.'"> '.$name.'</option>';
	$str = str_replace('value="'.$relayhost.'">','value="'.$relayhost.'" selected>', $str, $count);
	if( $count == 0 )
		echo '<option name="other" value="" selected> annan / ingen</option>'.$str;
	else
		echo '<option name="other" value=""> annan / ingen</option>'.$str;
	echo   '</select>';
	echo   ' -> <input type="text" name="relayhost" onKeyDown="document.pageform.isp.selectedIndex=0" value="'.$relayhost.'"></td></tr>';

	echo '   </td></tr>';
	
	echo "<tr><td>&nbsp;</td><td><input type=\"submit\" name=\"testmail\" value=\"Skicka testmail\" class=\"buttonlong\" ".help('admin_testmail')."></td></tr>";
	echo "</table>";
	echo '
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
<!--
function base_isp_update() {
        document.pageform.relayhost.value = document.pageform.isp.value
}
//-->
</SCRIPT>
';

	echo "<br><table ".help('password').">\n";
	echo '<tr><td colspan="2" class="tbhead">Ändra lösen för administratörssidorna</td></tr>';
	//echo "  <tr><td>gamla användarnamnet</td><td><input type=\"text\" class=\"input-box\" name=\"ousername\"></td></tr>\n";
	echo "  <tr><td>gamla lösenordet</td><td><input type=\"password\" class=\"input-box\" name=\"opasswd\"></td></tr>\n";
	//echo "  <tr><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
	//echo "  <tr><td>nya användarnamnet</td><td><input type=\"text\" class=\"input-box\" name=\"nusername\"></td></tr>\n";
	echo "  <tr><td>nya lösenordet</td><td><input type=\"password\" class=\"input-box\" name=\"npasswd1\"></td></tr>\n";
	echo "  <tr><td>repetera nya lösenordet</td><td><input type=\"password\" class=\"input-box\" name=\"npasswd2\"></td></tr>\n";
	echo "</table>\n";
}


function power()
{
	/*
	OMSTART OCH AVSTÄNGNING
	*/
	echo "<table>";
	echo "<tr><td><input type=\"submit\" name=\"b_halt\" value=\"Stäng av server\" class=\"buttonlong\" ".help('power_halt')." ".confirm('power_halt')."></td>";
	echo "<td><input type=\"submit\" name=\"b_reboot\" value=\"Starta om server\" class=\"buttonlong\" ".help('power_reboot')." ".confirm('power_reboot')."></td></tr>";
	echo "</table>";
}


////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

	html_start( array(
		array('timedate', 'Datum och tid'), 
		array('admin', 'Administratörsuppgifter'), 
		array('net', 'Nätverk'), 
		array('power', 'Omstart / Stäng av') 
	));

        $t='';
        if( isset($_GET['t']) )
            $t=$_GET['t'];

	switch( $t ) {
	case 'timedate':
		timedate();
		html_end(true, true, true);
		break;
	case 'net':
		net();
		html_end(true, true, true);
		break;
	case 'upgrade':
		upgrade();
		html_end(true, true, true);
		break;
	case 'admin':
		administrator();
		html_end(true, true, true);
		break;
	case 'power':
		power();
		html_end();
		break;
	default:
		timedate();
		html_end(true, true, true);
		break;
	}
?>
